package com.cn.zengzeng.spbzengzeng.utils;

import com.google.gson.Gson;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;

/**
 * @Description: 排序种类
 * @Author: ZhangZengZeng
 * @Date: 2022/7/6 14:28
 */
@Slf4j
public class VariableQiNiu {
    /**
     * ak
     */
    private static final String ACCESS_KEY = "oRAj0VHJUpDcJJVxBdHc-YchhxjQWMDtH1HR2Vnu";

    /**
     *  SK
     */
    private static final String SECRET_KEY = "XwqbHcCLNThImsUFV82Tu4zjs-Z0tB6NPq6L8Vl6";
    /**
     * 要上传的空间
     */
    public static final String BUCKET_NAME = "tomcat-zengzeng";
    /**
     * 域名
     */
    public static final String DOMAIN = "http://qij47a71b.hn-bkt.clouddn.com/";
    /**
     * 密钥配置
     */
    private static final Auth AUTH_KEY = Auth.create(ACCESS_KEY,SECRET_KEY);
    private static final Configuration CONFIGURATION = new Configuration(Region.huanan());
    /**
     * 创建上传对象
     */
    private static final UploadManager MAPPER = new UploadManager(CONFIGURATION);
    private VariableQiNiu() {
        throw new IllegalStateException("Utility class");
    }
    /**
  * @Author:  zhangzengzeng
  * @Date 2021/10/27 14:13
  * @param file 文件
  * @return java.lang.String
  */
    public static String uploadFile (MultipartFile file) {
        try {
            byte[] bytes = file.getBytes();
            String fileName = System.currentTimeMillis() + ".jpg";
            String token = AUTH_KEY.uploadToken(BUCKET_NAME);
            //上传
            Response response = MAPPER.put(bytes,fileName,token);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(),DefaultPutRet.class);
            log.info("putRet:{}",putRet.key);
            return VariableQiNiu.DOMAIN + fileName;
        }catch(IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    //随机生成字符串
    public static String getRandomCharacterAndNumber (int length) throws NoSuchAlgorithmException {

        StringBuilder val = new StringBuilder();
        Random random = SecureRandom.getInstanceStrong();
        for(int i = 0;i < length;i++) {
            //输出是字符还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";

            if("char".equalsIgnoreCase(charOrNum)){
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val.append((char) (choice + random.nextInt(26)));
                // int choice = 97; // 指定字符串为小写字母
                val.append((char) (choice + random.nextInt(26)));
                // 数字
            } else {
                val.append(random.nextInt(10));
            }
        }
        return val.toString();
    }

}
